package org.hibiscus.jdp.app.dbdoc.mysql;

import org.hibiscus.jdp.app.dbdoc.AbstractSqlUtils;
import org.hibiscus.jdp.app.dbdoc.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

/**
 * 描述：MySQL工具类
 *
 * @author rannuo
 * @date 2019/3/16
 */
public class MySqlUtils extends AbstractSqlUtils {
	private static final Logger log = LoggerFactory.getLogger(MySqlUtils.class);

	private static final String URL_PREFIX = "jdbc:mysql://";
	private static final String DEFAULT_URL_PARAM = "?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8";
	private static final String DEFAULT_URL = URL_PREFIX + "127.0.0.1:3306" + DEFAULT_URL_PARAM;

	static {
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			log.error("未找到匹配驱动", e);
		}
	}

	private static String buildUrl(String host, String tableSchema) {
		if (host != null && !"".equals(host)) {
			return URL_PREFIX + host + "/" + tableSchema + DEFAULT_URL_PARAM;
		}
		return DEFAULT_URL;
	}

	static List<Map<String, String>> getTableStructureData(DataSource dataSource, String sql, boolean isMetaInfo) {
		return getTableStructureData(buildUrl(dataSource.getHost(), dataSource.getTableSchema()),
				dataSource.getUsername(), dataSource.getPassword(), sql, isMetaInfo);
	}

}
